package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.dto.EventPlan;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventTh;
import com.ruoyi.event.domain.bo.EventThBo;
import com.ruoyi.event.domain.vo.EventThVo;
import com.ruoyi.event.mapper.EventThMapper;
import com.ruoyi.event.service.IEventThService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 谈话记录Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventThServiceImpl implements IEventThService {

    private final EventThMapper baseMapper;

    /**
     * 查询谈话记录
     */
    @Override
    public EventThVo queryById(Long thId) {
        return baseMapper.selectVoById(thId);
    }

    /**
     * 查询谈话记录列表
     */
    @Override
    public TableDataInfo<EventThVo> queryPageList(EventThBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventTh> lqw = buildQueryWrapper(bo);
        Page<EventThVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询谈话记录列表
     */
    @Override
    public List<EventThVo> queryList(EventThBo bo) {
        LambdaQueryWrapper<EventTh> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventTh> buildQueryWrapper(EventThBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventTh> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventTh::getUserId, bo.getUserId());
        lqw.eq(bo.getMemberId() != null, EventTh::getMemberId, bo.getMemberId());
        lqw.eq(StringUtils.isNotBlank(bo.getThlx()), EventTh::getThlx, bo.getThlx());
        lqw.eq(bo.getThrq() != null, EventTh::getThrq, bo.getThrq());
        lqw.eq(StringUtils.isNotBlank(bo.getThdz()), EventTh::getThdz, bo.getThdz());
        lqw.eq(StringUtils.isNotBlank(bo.getThgy()), EventTh::getThgy, bo.getThgy());
        lqw.eq(StringUtils.isNotBlank(bo.getThnr()), EventTh::getThnr, bo.getThnr());
        lqw.eq(StringUtils.isNotBlank(bo.getImg()), EventTh::getImg, bo.getImg());
        lqw.eq(StringUtils.isNotBlank(bo.getFj()), EventTh::getFj, bo.getFj());
        lqw.eq(StringUtils.isNotBlank(bo.getCyr()), EventTh::getCyr, bo.getCyr());
        return lqw;
    }

    /**
     * 新增谈话记录
     */
    @Override
    public Boolean insertByBo(EventThBo bo) {
        EventTh add = BeanUtil.toBean(bo, EventTh.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setThId(add.getThId());
        }
        return flag;
    }

    /**
     * 修改谈话记录
     */
    @Override
    public Boolean updateByBo(EventThBo bo) {
        EventTh update = BeanUtil.toBean(bo, EventTh.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventTh entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除谈话记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void renderEventInfo(Long userId, List<EventPlan> eventPlanList) {
        LambdaQueryWrapper<EventTh> lqw = Wrappers.lambdaQuery();
        lqw.eq(EventTh::getUserId, userId);
        lqw.orderByAsc(EventTh::getThrq);
        List<EventTh> eventThList = baseMapper.selectList(lqw);
        eventPlanList.forEach(eventPlan -> {
            eventThList.forEach(eventTh -> {
                if (eventPlan.isOccurInThisPlanCycle(eventTh.getThrq())) {
                    eventPlan.setEventId(eventTh.getThId());
                    eventPlan.setEventDate(eventTh.getThrq());
                }
            });
        });
    }
}
